<?php

if (!defined('IN_IA')) {
    exit('Access Denied');
}

class List_MxShopPage extends WebPage {

    public function main() {

        global $_W, $_GPC;
        $uniacid = $_W['platformid'];

        include $this->template();
    }

    public function channel(){
        global $_W, $_GPC;
        $uniacid = $_W['platformid'];

        $pindex = max(1, intval($_GPC['page']));
        $psize = 20;

        $condition = " c.channelid!='' AND c.deleted=0";

        if (!(empty($_GPC['searchfield'])) && !(empty($_GPC['keyword'])))
        {
            $paras[':keyword'] = htmlspecialchars_decode($_GPC['keyword'], ENT_QUOTES);
            if ($_GPC['searchfield'] == 'member'){
                $condition .= ' AND (locate(:keyword,m.realname)>0 or locate(:keyword,m.mobile)>0 or locate(:keyword,m.nickname)>0)';

            }else if ($_GPC['searchfield'] == 'mendian'){
                $condition .= ' AND locate(:keyword,c.mendianname)>0 ';

            }else if ($_GPC['searchfield'] == 'address'){
                $condition .= ' AND (locate(:keyword,c.province)>0 or locate(:keyword,c.city)>0 or locate(:keyword,c.dist)>0)';
            }
        }

        $list = pdo_fetchall("SELECT c.*, m.id as mid, m.mobile as mobile, m.realname as realname, m.avatar as avatar, m.province as province, m.city as city FROM " . tablename('mx_shop_mendian') . " c LEFT JOIN " . tablename('mx_shop_user') . " m ON c.channelid=m.id WHERE {$condition} GROUP BY c.channelid LIMIT " . ($pindex - 1) * $psize . ",{$psize}", $paras);

        foreach ($list as $key => $item){
            $list[$key]['mendian'] = pdo_fetchcolumn("SELECT count(id) FROM " . tablename('mx_shop_mendian') . " WHERE deleted=0 AND channelid=:channelid", array(':channelid'=>$item['channelid']) );
            $list[$key]['order'] = pdo_fetchcolumn("SELECT count(id) FROM " . tablename('mx_shop_service_order') . " WHERE uid=:uid", array(':uid'=>$item['channelid']) );
            $comm = pdo_fetch("SELECT id,total FROM " . tablename('mx_shop_service_commission') . " WHERE uid=:uid", array(':uid'=>$item['channelid']) );
            $list[$key]['comid'] = $comm['id'];
            $list[$key]['comm'] = $comm['total'] > 0 ? $comm['total'] : 0;
        }

        $total = pdo_fetchcolumn("SELECT count(id) FROM " . tablename('mx_shop_mendian') . " WHERE deleted=0 GROUP BY channelid" );
        $pager = pagination($total, $pindex, $psize);

        include $this->template();
    }

    public function manage(){
        global $_W, $_GPC;
        $uniacid = $_W['platformid'];

        $pindex = max(1, intval($_GPC['page']));
        $psize = 20;

        $condition = " c.manageid!='' AND c.deleted=0";

        if (!(empty($_GPC['searchfield'])) && !(empty($_GPC['keyword'])))
        {
            $paras[':keyword'] = htmlspecialchars_decode($_GPC['keyword'], ENT_QUOTES);
            if ($_GPC['searchfield'] == 'member'){
                $condition .= ' AND (locate(:keyword,m.realname)>0 or locate(:keyword,m.mobile)>0 or locate(:keyword,m.nickname)>0)';

            }else if ($_GPC['searchfield'] == 'mendian'){
                $condition .= ' AND locate(:keyword,c.mendianname)>0 ';

            }else if ($_GPC['searchfield'] == 'address'){
                $condition .= ' AND (locate(:keyword,c.province)>0 or locate(:keyword,c.city)>0 or locate(:keyword,c.dist)>0)';
            }
        }

        $list = pdo_fetchall("SELECT c.*, m.id as mid, m.mobile as mobile, m.realname as realname, m.avatar as avatar, m.province as province, m.city as city FROM " . tablename('mx_shop_mendian') . " c LEFT JOIN " . tablename('mx_shop_user') . " m ON c.manageid=m.id WHERE {$condition} GROUP BY c.manageid LIMIT " . ($pindex - 1) * $psize . ",{$psize}", $paras);
        foreach ($list as $key => $item){
            $list[$key]['mendian'] = pdo_fetchcolumn("SELECT count(id) FROM " . tablename('mx_shop_mendian') . " WHERE deleted=0 AND manageid=:manageid", array(':manageid'=>$item['manageid']) );
            $list[$key]['order'] = pdo_fetchcolumn("SELECT count(id) FROM " . tablename('mx_shop_service_order') . " WHERE uid=:uid", array(':uid'=>$item['manageid']) );
            $comm = pdo_fetch("SELECT id,total FROM " . tablename('mx_shop_service_commission') . " WHERE uid=:uid", array(':uid'=>$item['manageid']) );
            $list[$key]['comid'] = $comm['id'];
            $list[$key]['comm'] = $comm['total'] > 0 ? $comm['total'] : 0;

        }
        $total = pdo_fetchcolumn("SELECT count(id) FROM " . tablename('mx_shop_mendian') . " WHERE deleted=0 GROUP BY manageid" );
        $pager = pagination($total, $pindex, $psize);

        include $this->template();
    }

}
